package com.cade.agriculturemall.view.personalCenter.collect;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.GoodsDetailEntity;
import com.cade.agriculturemall.model.ItemFavouriteEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.List;

/**
 * Created by Lobby on 2017/12/4.
 */

public class CollectPresenter extends BasePresenter<CollectContract.IView> implements CollectContract.IPresenter {
    public CollectPresenter(CollectContract.IView view) {
        super(view);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void loadProducter() {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        String currentUserId = user.getObjectId();

        final AVQuery<ItemFavouriteEntity> itemEntity = new AVQuery<>("ItemFavourite");
        itemEntity.whereEqualTo("user_Id", currentUserId);
        itemEntity.include("pro_Id");
        itemEntity.findInBackground(new FindCallback<ItemFavouriteEntity>() {
            @Override
            public void done(List<ItemFavouriteEntity> list, AVException e) {
                if (e == null) {
                    List<ItemFavouriteEntity> itemFavouriteEntities=list;
                    if (itemFavouriteEntities.size()!=0) {
                        itemFavouriteEntities = list;
                    }
                    mView.receiveList(itemFavouriteEntities);
                }
            }
        });
    }

    @Override
    public void transformIntoShopCar(GoodsDetailEntity entity) {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        String currentUserId = user.getObjectId();

        final AVObject shoppingCars = new AVObject("ShoppingCars");
        shoppingCars.put("user_Id", currentUserId);
        shoppingCars.put("pro", entity);
        shoppingCars.put("quantitate", 1);
        shoppingCars.put("pro_price", entity.getPriceNew());
        shoppingCars.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    String itemCarsId = shoppingCars.getObjectId();
                }
            }
        });
    }

    @Override
    public void delete(String collectId) {
        AVQuery<AVObject> deleteFavourite = new AVQuery<>("ItemFavourite");
        deleteFavourite.whereEqualTo("objectId", collectId);
        deleteFavourite.deleteAllInBackground(new DeleteCallback() {
            @Override
            public void done(AVException e) {
                if (e != null) {
                    mView.showMessage(e.getMessage());
                }
            }
        });
    }

}
